<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="cn.ecut.model.Good" %>
<%@ page import="cn.ecut.model.User" %>
<%@ page import="cn.ecut.services.GoodServices" %>
<%@ page import="java.util.List" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Collections" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    if (session.getAttribute("user") == null) {
        response.sendRedirect("/login.jsp");
        return;
    }
    User user=(User)session.getAttribute("user");
//    User user = new User(1, "zhangsan", "qweasd", "123456", "男", "zhangsan@qq.com");
    GoodServices goodservice =new GoodServices();
%>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <title>我的发布</title>
    <link href="${pageContext.request.contextPath}/css/bootstrap.min.css" type="text/css" rel="stylesheet">
    <script src="${pageContext.request.contextPath}/js/jquery-3.5.1.min.js"></script>
</head>

<body>
    <div class="container">
        <div><h2> 我的发布</h2><a href="${pageContext.request.contextPath}/applymanage.jsp"><input type="button" value="管理我的发布" ></a>
        <div>你好！<div id="username"></div></div>
        </div>
        <table class="table table-hover text-center">
            <tr>
                <th width="10%">序号</th>
                <th width="15%">物品图片预览</th>
                <th width="15%">物品标题</th>
                <th width="15%">发布时间</th>
                <th width="10%">物品状态</th>
                <th width="15%">操作</th>
            </tr>
			<%
				List<Good> list= goodservice.loadByUserId(user.getUserid());
				if(list!=null && list.size()>0){
					int count=0;
					for(int i=0;i<list.size();i++){
						count++;
						Good g=list.get(i);
						SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
               			String fabutime = sdf1.format(g.getFabutime());
		            	String goodstate="";
		            	if("0".equals(g.getGoodstate())){
		            		goodstate="等待认领中";
		            	}else if("1".equals(g.getGoodstate())){
		            		goodstate="<font color='#ff0000'>已物归原主</font>";
		            	}
			%>
            <tr>
                <td><%=count %></td>
                <td height="100px"><img src="${pageContext.request.contextPath}<%=g.getGoodpicture() %>" alt="图片" width="100%" height="100%" /></td>
                <td><%=g.getGoodname() %></td>
                <td><%=fabutime %></td>
                <td><%=goodstate %></td>
                <td>
                    <%
                        if("0".equals(g.getGoodstate())){
                    %>
                        <input type="button" name="" id="button2" onclick="chg(<%=g.getGoodid() %>)" value="归还">
                    <%
                        }
                    %>
                    <input type="button" name="" id="button1" onclick="del(<%=g.getGoodid() %>)" value="删除">
                </td>
            </tr>
            <%
            		}
            	}
            %>
        </table>
    </div>
    <script type="text/javascript">
        $(function () {
            $.post("${pageContext.request.contextPath}/user/getInfo" , function(responseData){
                if( responseData.status == -1 ){
                    $("#username").css("display" , "none") ;
                }else if(responseData.status == 0){
                    $("#username").css("display" , "block") ;
                    $("#username").text(responseData.data.username) ;
                }
            } , "JSON") ;
        });
        function del(id) {
            if (confirm("您确定要删除吗?")) {
                window.location.href="${pageContext.request.contextPath}/lost/deal?deletegoodid="+id;
            }
        }
        function chg(id) {
            if (confirm("您确定已归还吗?")) {
                window.location.href="${pageContext.request.contextPath}/lost/deal?changegoodid="+id;
            }
        }
    </script>
</body>

</html>

